﻿#include "AbstractItemViewPropertyEditEntry.h"
#include <QAbstractItemView>

#include "BooleanEditEntry.h"
#include "IntegerEditEntry.h"
#include "EnumEditEntry.h"
#include "SizeEditEntry.h"

#include "ObjectPropertyEditEntryFactory.h"
REGISTER_PROPERTY_EDIT_ENTRY(AbstractItemView)

struct AbstractItemViewPropertyEditEntry::AbstractItemViewPropertyEditEntryPrivate {
    QPointer<QAbstractItemView> view;

    QPointer<BooleanEditEntry> entryAutoScroll;
    QPointer<IntegerEditEntry> entryAutoScrollMargin;
    QPointer<EnumEditEntry> entryEditTriggers;
    QPointer<BooleanEditEntry> entryTabKeyNavigation;
    QPointer<BooleanEditEntry> entryShowDropIndicator;
    QPointer<BooleanEditEntry> entryDragEnabled;
    QPointer<BooleanEditEntry> entryDragDropOverwriteMode;
    QPointer<EnumEditEntry> entryDragDropMode;
    QPointer<EnumEditEntry> entryDefaultDropAction;
    QPointer<BooleanEditEntry> entryAlternatingRowColors;
    QPointer<EnumEditEntry> entrySelectionMode;
    QPointer<EnumEditEntry> entrySelectionBehavior;
    QPointer<SizeEditEntry> entryIconSize;
    QPointer<EnumEditEntry> entryTextElideMode;
    QPointer<EnumEditEntry> entryVerticalScrollMode;
    QPointer<EnumEditEntry> entryHorizontalScrollMode;
};

AbstractItemViewPropertyEditEntry::~AbstractItemViewPropertyEditEntry() { }

AbstractItemViewPropertyEditEntry::AbstractItemViewPropertyEditEntry(QWidget *parent) : PropertyEditEntry{parent} {
    p.reset(new AbstractItemViewPropertyEditEntryPrivate);
    initUi();
    retranslateUi();
}

AbstractItemViewPropertyEditEntry::AbstractItemViewPropertyEditEntry(void *object, QWidget *parent) : PropertyEditEntry{parent} {
    p.reset(new AbstractItemViewPropertyEditEntryPrivate);
    initUi();
    retranslateUi();
    setObject(object);
}

void AbstractItemViewPropertyEditEntry::setObject(void *object) {
    p->view = static_cast<QAbstractItemView*>(object);
}

void AbstractItemViewPropertyEditEntry::onTick() {
    const auto &view = p->view;
    if(!view || !isVisible()) {
        return ;
    }

    p->entryAutoScroll->setValue(view->hasAutoScroll());
    p->entryAutoScrollMargin->setValue(view->autoScrollMargin());
    p->entryEditTriggers->setValue(view->editTriggers());
    p->entryTabKeyNavigation->setValue(view->tabKeyNavigation());
    p->entryShowDropIndicator->setValue(view->showDropIndicator());
    p->entryDragEnabled->setValue(view->dragEnabled());
    p->entryDragDropOverwriteMode->setValue(view->dragDropOverwriteMode());
    p->entryDragDropMode->setValue(view->dragDropMode());
    p->entryDefaultDropAction->setValue(view->defaultDropAction());
    p->entryAlternatingRowColors->setValue(view->alternatingRowColors());
    p->entrySelectionMode->setValue(view->selectionMode());
    p->entrySelectionBehavior->setValue(view->selectionBehavior());
    p->entryIconSize->setValue(view->iconSize());
    p->entryTextElideMode->setValue(view->textElideMode());
    p->entryVerticalScrollMode->setValue(view->verticalScrollMode());
    p->entryHorizontalScrollMode->setValue(view->horizontalScrollMode());
}

void AbstractItemViewPropertyEditEntry::initUi() {
    p->entryAutoScroll = new BooleanEditEntry;
    appendChildEntry(p->entryAutoScroll);

    p->entryAutoScrollMargin = new IntegerEditEntry;
    appendChildEntry(p->entryAutoScrollMargin);

    p->entryEditTriggers = new EnumEditEntry;
    p->entryEditTriggers->setMetaEnum(QMetaEnum::fromType<QAbstractItemView::EditTriggers>());
    appendChildEntry(p->entryEditTriggers);

    p->entryTabKeyNavigation = new BooleanEditEntry;
    appendChildEntry(p->entryTabKeyNavigation);

    p->entryShowDropIndicator = new BooleanEditEntry;
    appendChildEntry(p->entryShowDropIndicator);

    p->entryDragEnabled = new BooleanEditEntry;
    appendChildEntry(p->entryDragEnabled);

    p->entryDragDropOverwriteMode = new BooleanEditEntry;
    appendChildEntry(p->entryDragDropOverwriteMode);

    p->entryDragDropMode = new EnumEditEntry;
    p->entryDragDropMode->setMetaEnum(QMetaEnum::fromType<QAbstractItemView::DragDropMode>());
    appendChildEntry(p->entryDragDropMode);

    p->entryDefaultDropAction = new EnumEditEntry;
    p->entryDefaultDropAction->setMetaEnum(QMetaEnum::fromType<Qt::DropAction>());
    appendChildEntry(p->entryDefaultDropAction);

    p->entryAlternatingRowColors = new BooleanEditEntry;
    appendChildEntry(p->entryAlternatingRowColors);

    p->entrySelectionMode = new EnumEditEntry;
    p->entrySelectionMode->setMetaEnum(QMetaEnum::fromType<QAbstractItemView::SelectionMode>());
    appendChildEntry(p->entrySelectionMode);

    p->entrySelectionBehavior = new EnumEditEntry;
    p->entrySelectionBehavior->setMetaEnum(QMetaEnum::fromType<QAbstractItemView::SelectionBehavior>());
    appendChildEntry(p->entrySelectionBehavior);

    p->entryIconSize = new SizeEditEntry;
    appendChildEntry(p->entryIconSize);

    p->entryTextElideMode = new EnumEditEntry;
    p->entryTextElideMode->setMetaEnum(QMetaEnum::fromType<Qt::TextElideMode>());
    appendChildEntry(p->entryTextElideMode);

    p->entryVerticalScrollMode = new EnumEditEntry;
    p->entryVerticalScrollMode->setMetaEnum(QMetaEnum::fromType<QAbstractItemView::ScrollMode>());
    appendChildEntry(p->entryVerticalScrollMode);

    p->entryHorizontalScrollMode = new EnumEditEntry;
    p->entryHorizontalScrollMode->setMetaEnum(QMetaEnum::fromType<QAbstractItemView::ScrollMode>());
    appendChildEntry(p->entryHorizontalScrollMode);
}

void AbstractItemViewPropertyEditEntry::retranslateUi() {
    setTitle("QAbstractItemView");
    p->entryAutoScroll->setTitle(tr("autoScroll"));
    p->entryAutoScrollMargin->setTitle(tr("autoScrollMargin"));
    p->entryEditTriggers->setTitle(tr("editTriggers"));
    p->entryTabKeyNavigation->setTitle("tabKeyNavigation");
    p->entryShowDropIndicator->setTitle(tr("showDropIndicator"));
    p->entryDragEnabled->setTitle(tr("dragEnabled"));
    p->entryDragDropOverwriteMode->setTitle(tr("dragDropOverwriteMode"));
    p->entryDragDropMode->setTitle(tr("dragDropMode"));
    p->entryDefaultDropAction->setTitle(tr("defaultDropAction"));
    p->entryAlternatingRowColors->setTitle(tr("alternatingRowColors"));
    p->entrySelectionMode->setTitle(tr("selectionMode"));
    p->entrySelectionBehavior->setTitle(tr("selectionBehavior"));
    p->entryIconSize->setTitle(tr("iconSize"));
    p->entryTextElideMode->setTitle(tr("textElideMode"));
    p->entryVerticalScrollMode->setTitle(tr("verticalScrollMode"));
    p->entryHorizontalScrollMode->setTitle(tr("horizontalScrollMode"));
}
